﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.IO;
using System.Text;
using Smartkernel.Attributes;
using Smartkernel.Configuration;
using Smartkernel.IO;
using Smartkernel.Xml;

namespace Smartkernel.Net
{
    /// <summary>
    /// 邮件发送者实体类
    /// </summary>
    public partial class SmartMailSenderEntity
    {
        #region 公有属性
        private string smtp;
        /// <summary>
        /// Smtp服务器地址
        /// </summary>
        [SmartXmlMappingAttribute("Smtp")]
        public string Smtp
        {
            get
            {
                return this.smtp;
            }
            set
            {
                this.smtp = value;
            }
        }
        private int port;
        /// <summary>
        /// Smtp服务器端口
        /// </summary>
        [SmartXmlMappingAttribute("Port")]
        public int Port
        {
            get
            {
                return this.port;
            }
            set
            {
                this.port = value;
            }
        }
        private string senderEmail;
        /// <summary>
        /// 发送者邮箱
        /// </summary>
        [SmartXmlMappingAttribute("SenderEmail")]
        public string SenderEmail
        {
            get
            {
                return this.senderEmail;
            }
            set
            {
                this.senderEmail = value;
            }
        }
        private string senderUserName;
        /// <summary>
        /// 发送者用户名
        /// </summary>
        [SmartXmlMappingAttribute("SenderUserName")]
        public string SenderUserName
        {
            get
            {
                return this.senderUserName;
            }
            set
            {
                this.senderUserName = value;
            }
        }
        private string senderPassword;
        /// <summary>
        /// 发送者密码
        /// </summary>
        [SmartXmlMappingAttribute("SenderPassword")]
        public string SenderPassword
        {
            get
            {
                return this.senderPassword;
            }
            set
            {
                this.senderPassword = value;
            }
        }
        private int timeout;
        /// <summary>
        /// 发送超时时间，但是为秒
        /// </summary>
        [SmartXmlMappingAttribute("Timeout")]
        public int Timeout
        {
            get
            {
                return this.timeout;
            }
            set
            {
                this.timeout = value;
            }
        }
        private static SmartMailSenderEntity instance;
        /// <summary>
        /// 通过配置文件初始化的默认实例
        /// </summary>
        public static SmartMailSenderEntity Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion
    }
    public partial class SmartMailSenderEntity
    {
        #region 构造函数及字段
        /// <summary>
        /// 私有构造函数
        /// </summary>
        private SmartMailSenderEntity()
        { 
            
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="smtp">Smtp服务器地址</param>
        /// <param name="port">Smtp服务器端口</param>
        /// <param name="senderEmail">发送者邮箱</param>
        /// <param name="senderUserName">发送者用户名</param>
        /// <param name="senderPassword">发送者密码</param>
        /// <param name="timeout">发送超时时间</param>
        public SmartMailSenderEntity(string smtp, int port, string senderEmail, string senderUserName, string senderPassword, int timeout)
        {
            this.smtp = smtp;
            this.port = port;
            this.senderEmail = senderEmail;
            this.senderUserName = senderUserName;
            this.senderPassword = senderPassword;
            this.timeout = timeout;
        }
        private static string xmlPath;
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static SmartMailSenderEntity()
        {
            if (SmartAction.IsInvokeFail(delegate { xmlPath = SmartConfiger.GetAppSettingValue<string>("ConfigFolder", true).TrimEnd(new char[] { '\\' }) + @"\SmartMailSenderEntity.config"; }))
            {
                xmlPath = Environment.CurrentDirectory + @"\SmartMailSenderEntity.config";
                if (!File.Exists(xmlPath))
                {
                    xmlPath = System.Web.HttpRuntime.AppDomainAppPath.TrimEnd('\\') + @"\ConfigFolder\SmartMailSenderEntity.config";
                }
            }
            //存在配置文件的时候才监视文件的变化
            if (File.Exists(xmlPath))
            {
                instance = new SmartMailSenderEntity();
                SmartXml.MappingXO<SmartMailSenderEntity>(xmlPath, SmartMappingType.Attribute, ref instance);
                SmartFileSystemWatcher.Add(xmlPath, delegate { SmartXml.MappingXO<SmartMailSenderEntity>(xmlPath, SmartMappingType.Attribute, ref instance); });
            }
        }
        #endregion
    }
    public partial class SmartMailSenderEntity
    {
        #region 覆盖基类的方法
        /// <summary>
        /// 比较两个对象是否相等
        /// </summary>
        /// <param name="obj">要比较的对象</param>
        /// <returns>比较的结果</returns>
        public override bool Equals(object obj)
        {
            SmartMailSenderEntity other = obj as SmartMailSenderEntity;
            if (other == null)
            {
                return false;
            }
            else
            {
                return this.Smtp == other.Smtp && this.SenderUserName == other.SenderUserName && this.SenderPassword == other.SenderPassword && this.Timeout == other.Timeout;
            }
        }
        /// <summary>
        /// 获得哈希码
        /// </summary>
        /// <returns>哈希码</returns>
        public override int GetHashCode()
        {
            return this.ToString().GetHashCode();
        }
        /// <summary>
        /// 获得字符串表示
        /// </summary>
        /// <returns>输出的字符串</returns>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder(50);
            stringBuilder.Append("SmartMailSenderEntity");
            if (this.Smtp != null)
            {
                stringBuilder.Append(this.Smtp.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.SenderUserName != null)
            {
                stringBuilder.Append(this.SenderUserName.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.SenderPassword != null)
            {
                stringBuilder.Append(this.SenderPassword.GetHashCode().ToString());
            }
            stringBuilder.Append("__");
            if (this.Timeout != 0)
            {
                stringBuilder.Append(this.Timeout.GetHashCode().ToString());
            }
            return stringBuilder.ToString();
        }
        #endregion
    }
}
